package com.zheinfo.web.controller.biz;

import com.zheinfo.biz.domain.Cart;
import com.zheinfo.biz.service.ICartService;
import com.zheinfo.common.core.controller.BaseController;
import com.zheinfo.common.core.domain.AjaxResult;
import com.zheinfo.common.core.page.TableDataInfo;
import com.zheinfo.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 购物车Controller
 * 
 * @author zheinfo
 */
@RestController
@RequestMapping("/cart")
public class CartController extends BaseController
{
    @Autowired
    private ICartService cartService;

    /**
     * 查询购物车列表
     */
    @GetMapping("/list")
    public TableDataInfo list(Cart cart)
    {
        // 设置当前登录用户ID
        cart.setUserId(SecurityUtils.getUserId());
        startPage();
        List<Cart> list = cartService.selectCartList(cart);
        return getDataTable(list);
    }

    /**
     * 获取购物车详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
      return   success(cartService.selectCartById(id));
    }

    /**
     * 新增购物车
     */
    @PostMapping
    public AjaxResult add(@RequestBody Cart cart)
    {
        // 设置当前登录用户ID
        cart.setUserId(SecurityUtils.getUserId());
        return toAjax(cartService.insertCart(cart));
    }

    /**
     * 修改购物车
     */
    @PutMapping
    public AjaxResult edit(@RequestBody Cart cart)
    {
        return toAjax(cartService.updateCart(cart));
    }

    /**
     * 删除购物车
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cartService.deleteCartByIds(ids));
    }
}